class CycleThing extends WallMode {
  PImage points = createImage(48, 48, RGB);
  float zagrate = 20;
  boolean cool = true;
  float bright = 255;
  CycleThing(PApplet parent) {
  }
  void keyPressed() {
  }
  void modeSelected() {
    points.pixels[0] = color(int(random(1, 255)), int(random(1, 255)), int(random(1, 255)));
  }

  int getDisplayTime() {
    return 10;
  }
  void setup() {
    background(0);
    colorMode(RGB, 255);

    points.pixels[0] = color(25, 177, 198);
    points.updatePixels();
  }

  float zag(float x) {
    // zagrate can be global so that we can munge it with keypresses!
    //  float zagrate = 8;
    if (x%2 == 0) {   
      if (x <= bright-zagrate) { 
        x = x+zagrate;
      }
      else { 
        x--;
      }
    }
    else {
      if (x >=1+zagrate) { 
        x=x-zagrate;
      }
      else {
        x++ ;
      }
    }
    return x;
  }

  void cycle1 (PImage foo) {
    //recolor array of points
    foo.loadPixels();
    for (int p=1; p <= 48*48 ; p++) {
      if (p != 1) {
        foo.pixels[p-1] = color(zag(red(foo.pixels[p-2])), zag(green(foo.pixels[p-2])), zag(blue(foo.pixels[p-2])));
      }
      else {
        foo.pixels[0] = color(zag(red(foo.pixels[0])), zag(green(foo.pixels[0])), zag(blue(foo.pixels[0])));
      }
    }
    foo.updatePixels();
  }

  PImage coolify(PImage corge) {
    corge.loadPixels();
    PImage grault = createImage(48, 48, RGB);
    grault.loadPixels();
    for (int x=1; x <= 48 ; x++) {
      for (int y=1; y <= 48 ; y++) {
        grault.pixels[48*y+x-49] = corge.pixels[(x*y-1)] ;
      }
    }
    grault.updatePixels();
    return grault;
  }

  void draw() {
    //recolor array of points
    cycle1(points);
    //draw array of points onto screen as stuff
    image(coolify(points), 0, 0);
  }
  int getNewModeIdentifier() { 
    return 1;
  }
}

